package com.jy.datapipeline.export.dao.impl;

import com.jy.datapipeline.common.constants.ExpConstants;
import com.jy.datapipeline.common.util.AssertUtil;
import com.jy.datapipeline.export.dao.ExpTaskExecuteLogDao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

@Slf4j
@Repository
public class ExpTaskExecuteLogDaoImpl implements ExpTaskExecuteLogDao {
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Override
    public List<Map<String, Object>> findExpTaskExecuteLogByList(String taskId, int iDisplayStart,
        int iDisplayLength,String status ) {
        List<Map<String, Object>> list=new ArrayList<>();
        try {
            StringBuffer sb=new StringBuffer();
            Map<String,Object> params = new HashMap<String,Object>();
            sb.append("SELECT a.id,a.task_id,a.task_executelog_id,a.status,"
                + " DATE_FORMAT(a.update_time,'%Y-%m-%d %H:%i:%s') as update_time,a.sum_count,a.verify_path,a.message from exp_task_execute_log a   "
                + " where 1=1  ");
            if(taskId!=null && !taskId.equals("")){
                sb.append(" and a.task_id =:taskId");
                params.put("taskId",taskId);
            }
            if(status!=null && !status.equals("")){
                sb.append(" and a.status =:status");
                params.put("status",Integer.parseInt(status));
            }
            sb.append(" order by a.update_time desc  ");
            if(iDisplayStart>-1 && iDisplayLength>-1){
                sb.append("LIMIT  :iDisplayStart ,:iDisplayLength ;");
                params.put("iDisplayStart",iDisplayStart);
                params.put("iDisplayLength",iDisplayLength);
            }
            list=this.namedParameterJdbcTemplate.queryForList(sb.toString(),params);
        }catch (Exception e){
            log.error("findExpTaskExecuteLogByList exception",e);
        }

        return list;
    }

    @Override
    public int findExpTaskExecuteLogByCount(String taskId,String status) {
        int count=0;
        List<Map<String, Object>> list=new ArrayList<>();
        try {
            StringBuffer sb=new StringBuffer();
            Map<String,Object> params = new HashMap<String,Object>();
            sb.append("SELECT COUNT(a.id) as count from exp_task_execute_log a   "
                + " where 1=1  ");
            if(taskId!=null && !taskId.equals("")){
                sb.append(" and a.task_id =:taskId");
                params.put("taskId",taskId);
            }
            if(status!=null && !status.equals("")){
                sb.append(" and a.status =:status");
                params.put("status",Integer.parseInt(status));
            }
            sb.append(" order by a.update_time desc  ");
            list=this.namedParameterJdbcTemplate.queryForList(sb.toString(),params);
            if(list.size()>0){
                count=Integer.parseInt(list.get(0).get("count")!=null?list.get(0).get("count").toString():"0");
            }
        }catch (Exception e){
            log.error("findExpTaskExecuteLogByList exception",e);
        }
        return count;
    }

    @Override
    public boolean deleteByTaskId(String taskId) {
        Map<String,Object> params=new HashMap<>();
        String sql="delete from exp_task_execute_log where task_id=:taskId";
        params.put("taskId",taskId);
        int req=this.namedParameterJdbcTemplate.update(sql,params);
        return req>0?true:false;
    }

    @Override
    public boolean updateExeuceStatus(String taskExecuteId, int status,int sumCount,
            String verifyPath,String message) {

        Map<String,Object> params=new HashMap<>();
        StringBuffer sb=new StringBuffer();
        sb.append("update exp_task_execute_log  set status=:status,update_time=NOW()");
        if(sumCount>0){
            sb.append(" ,sum_count=:sumCount");
            params.put("sumCount",sumCount);
        }
        if(verifyPath!=null && !verifyPath.equals("")){
            sb.append(" ,verify_path=:verify_path");
            params.put("verify_path",verifyPath);
        }
        if(message!=null && !message.equals("")){
            sb.append(",message=:message");
            params.put("message",message);
        }
       sb.append(" where task_executelog_id=:task_executelog_id");

        params.put("task_executelog_id",taskExecuteId);
        params.put("status",status);
        int req=this.namedParameterJdbcTemplate.update(sb.toString(),params);
        return req>0?true:false;
    }

    @Override
    public boolean deleteByTaskExecutelogId(String taskExecutelogId) {

        Map<String,Object> params=new HashMap<>();
        String sql="delete from exp_task_execute_log where task_executelog_id=:taskExecutelogId";
        params.put("taskExecutelogId",taskExecutelogId);
        int req=this.namedParameterJdbcTemplate.update(sql,params);
        return req>0?true:false;
    }
}
